package com.fadadianqi.logistics.admin.controller;

import com.fadadianqi.logistics.admin.StatisticsService;
import com.fadadianqi.commons.ConfigHelper;
import com.fadadianqi.goods.model.Brand;
import com.fadadianqi.goods.model.Category;
import com.fadadianqi.goods.service.GoodsService;
import com.fadadianqi.logistics.inventory.model.Item;
import com.fadadianqi.logistics.inventory.service.InventoryService;
import com.fadadianqi.logistics.sell.service.BillService;
import com.yeqiangwei.commons.util.ArithHelper;
import com.yeqiangwei.commons.util.DateTimeUtils;
import com.yeqiangwei.commons.util.ParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Controller
public class StatisticsController {

    private static final String VIEW = "/admin/stat/index";
    private static final String VIEW_MONTH = "/admin/stat/month";
    private static final String VIEW_MONTH_COUNT = "/admin/stat/monthCount";
    private static final String VIEW_STORE = "/admin/stat/store";
    private static final String VIEW_STORE_COUNT = "/admin/stat/storeCount";

    @RequestMapping(value = "/a/stat/ajax/stat")
    public String ajaxStat(HttpServletRequest request, ModelMap modelMap) {

        int thisyear = DateTimeUtils.getCurrentYear();
        int year = com.yeqiangwei.commons.util.ParamHelper.getInteger(request, "year", thisyear);

        Double totalInventory = statisticsService.totalInventory(); //货物成本总额
        Double totalPurchase = statisticsService.totalPurchase(); //进货现金交易总和
        Double totalSalesYear = statisticsService.totalSales(year);
        Double totalCostYear = statisticsService.totalCost(year);
        Double totalPurchaseYear = statisticsService.totalPurchase(year); //
        Double inMoney = statisticsService.inMoney(year);
        Double outMoney = statisticsService.outMoney(year);

        modelMap.put("totalPurchase", totalPurchase);
        modelMap.put("totalInventory", totalInventory);
        modelMap.put("totalSalesYear", totalSalesYear);
        modelMap.put("totalCostYear", totalCostYear);
        modelMap.put("totalPurchaseYear", totalPurchaseYear);
        modelMap.put("inMoney", inMoney);
        modelMap.put("outMoney", outMoney);
        modelMap.put("money", ArithHelper.add(inMoney, outMoney));

        return "/admin/stat/ajaxStat";
    }

    @RequestMapping(value = "/a/stat")
    public String show(HttpServletRequest request, ModelMap modelMap) {

        int yearS = ConfigHelper.getInt("init.year", DateTimeUtils.getCurrentYear());
        int yearE = DateTimeUtils.getCurrentYear();
        if (ConfigHelper.getString("init.year") == null) {
            ConfigHelper.set("init.year", String.valueOf(yearS));
        }
        modelMap.put("yearS", yearS);
        modelMap.put("yearE", yearE);
        modelMap.put("yearC", DateTimeUtils.getCurrentYear());

        return VIEW;
    }

    @RequestMapping(value = "/a/stat/ajax/monthCount")
    public String monthCount(HttpServletRequest request, ModelMap modelMap) {
        int year = ParamUtils.getInt(request, "year", DateTimeUtils.getCurrentYear());
        List<Category> clist = goodsService.findAllCategory();
        List<StatMonthCategoryWrapper> alist = new ArrayList<StatMonthCategoryWrapper>();
        for (Category category : clist) {
            List<StatMonthCategory> list = new ArrayList<StatisticsController.StatMonthCategory>();
            for (int m = 0; m < 12; m++) {
                StatMonthCategory smc = new StatMonthCategory();
                Date start = DateTimeHelper.getOfMonthStart(year, m);
                Date end = DateTimeHelper.getOfMonthEnd(year, m);
                int num = billService.countItemBetween(category, start, end);
                smc.setNum(num);
                list.add(smc);
            }
            StatMonthCategoryWrapper s = new StatMonthCategoryWrapper();
            s.setCategory(category);
            s.setList(list);
            alist.add(s);
        }
        modelMap.put("command", alist);
        return VIEW_MONTH_COUNT;
    }

    @RequestMapping(value = "/a/stat/month")
    public String month(HttpServletRequest request, ModelMap modelMap) {
        int yearS = ConfigHelper.getInt("init.year", DateTimeUtils.getCurrentYear());
        int yearE = DateTimeUtils.getCurrentYear();
        if (ConfigHelper.getString("init.year") == null) {
            ConfigHelper.set("init.year", String.valueOf(yearS));
        }
        modelMap.put("yearS", yearS);
        modelMap.put("yearE", yearE);
        modelMap.put("yearC", DateTimeUtils.getCurrentYear());
        return VIEW_MONTH;
    }

    @RequestMapping(value = "/a/stat/store")
    public String store(HttpServletRequest request, ModelMap modelMap) {
        return VIEW_STORE;
    }

    @RequestMapping(value = "/a/stat/ajax/storeCount")
    public String storeCount(HttpServletRequest request, ModelMap modelMap) {
        List<Brand> brandList = goodsService.findAllBrand();
        List<Category> categoryList = goodsService.findAllCategory();
        List<StatStore> list = new ArrayList<StatisticsController.StatStore>();
        for (Category category : categoryList) {
            String categoryId = category.getId();
            for (Brand brand : brandList) {
                String brandId = brand.getId();
                int c = inventoryService.countByBrandIdAndCategoryId(brandId,
                        categoryId, Item.STATUS_NORMAL);
                if (c <= 0) {
                    continue;
                }
                double sumCost = inventoryService.sumCostByBrandIdAndCategoryId(
                        brandId, categoryId, Item.STATUS_NORMAL);
                double sumPurchase = inventoryService.sumPurchaseByBrandIdAndCategoryId(
                        brandId, categoryId, Item.STATUS_NORMAL);
                StatStore ss = new StatStore();
                ss.setBrand(brand);
                ss.setCategory(category);
                ss.setNum(c);
                ss.setPurchase(sumPurchase);
                ss.setCost(sumCost);
                list.add(ss);
            }
        }
        modelMap.addAttribute("command", list);
        return VIEW_STORE_COUNT;
    }

    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private BillService billService;

    @Autowired
    private InventoryService inventoryService;

    /**
     * 某品牌&某分类下的库存数量
     *
     * @author aroot
     */
    public static class StatStore implements Comparator<StatStore> {

        private Category category;
        private Brand brand;
        private int num;
        private Double purchase; // 进货价格
        private Double cost; // 成本

        public Double getPurchase() {
            return purchase;
        }

        public void setPurchase(Double purchase) {
            this.purchase = purchase;
        }

        public Double getCost() {
            return cost;
        }

        public void setCost(Double cost) {
            this.cost = cost;
        }

        public Brand getBrand() {
            return brand;
        }

        public void setBrand(Brand brand) {
            this.brand = brand;
        }

        public Category getCategory() {
            return category;
        }

        public void setCategory(Category category) {
            this.category = category;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        @Override
        public int compare(StatStore o1, StatStore o2) {
            if (o1.getNum() < o2.getNum()) {
                return 0;
            }
            return 1;
        }

    }

    public static class StatMonthCategoryWrapper {

        private Category category;
        private List<StatMonthCategory> list;

        public Category getCategory() {
            return category;
        }

        public void setCategory(Category category) {
            this.category = category;
        }

        public List<StatMonthCategory> getList() {
            return list;
        }

        public void setList(List<StatMonthCategory> list) {
            this.list = list;
        }
    }

    public static class StatMonthCategory {

        private int num; // 销售数量
        private double money; // 销售金额
        private double profit; // 毛利润

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }

        public double getMoney() {
            return money;
        }

        public void setMoney(double money) {
            this.money = money;
        }

        public double getProfit() {
            return profit;
        }

        public void setProfit(double profit) {
            this.profit = profit;
        }
    }

}
